<template>
  <div>
    <el-form ref="form" :model="personInfo" label-width="80px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="personInfo.name"></el-input>
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="personInfo.sex" placeholder="请选择">
          <el-option label="女" :value="0"></el-option>
          <el-option label="男" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="出生日期" prop="birthdate">
        <el-date-picker
          v-model="personInfo.birthdate"
          type="date"
          placeholder="选择日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="简介" prop="introduction">
        <el-input
          type="textarea"
          v-model="personInfo.introduction"
          :rows="5"
          maxlength="200"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      personInfo: {
        name: "",
        sex: 1,
        birthdate: new Date(),
        introduction: "",
      },
    };
  },
  created() {
    this.getPersonInfo();
  },
  methods: {
    //提交用户信息
    submit() {
      this.updatePersonInfo();
    },
    //获取个人信息
    getPersonInfo() {
      this.$request.getHttp({
        url: "/personinfo/getPersonInfoByUserId",
        params: {
          userId: localStorage.getItem("userId"),
        },
        success: (data) => {
          data.personInfo.birthdate = new Date(data.personInfo.birthdate);
          this.personInfo = data.personInfo;
        },
        error: (error) => {},
      });
    },
    //更新个人信息
    updatePersonInfo() {
      let personInfo = this.personInfo;
      personInfo.userid = localStorage.getItem("userId");
      console.log(personInfo);
      this.$request.postHttp({
        url: "/personinfo/updatePersonInfoByUserId",
        data: personInfo,
        success: (data) => {
          if (data.isUpdate) {
            this.$message({
              message: "已成功提交并更新",
              type: "success",
            });
            this.getPersonInfo();
          }
        },
        error: (error) => {
            this.$message({
              message: "提交失败",
              type: "error",
            });
        },
      });
    },
  },
};
</script>

<style scoped>
</style>